package com.oauth.google.server.orkut.util;

import java.util.ArrayList;
import java.util.List;

import com.google.orkut.client.api.BirthdayNotificationEntry;
import com.google.orkut.client.api.BirthdayNotificationTx;
import com.google.orkut.client.api.GetFriendTx;
import com.google.orkut.client.api.GetScrapsTx;
import com.google.orkut.client.api.OrkutPerson;
import com.google.orkut.client.api.ScrapEntry;
import com.oauth.google.client.beans.orkut.OrkutBirthday;
import com.oauth.google.client.beans.orkut.OrkutBirthdayDetails;
import com.oauth.google.client.beans.orkut.OrkutFriendDetails;
import com.oauth.google.client.beans.orkut.OrkutScrap;
import com.oauth.google.client.beans.orkut.OrkutScrapDetails;
import com.oauth.google.client.beans.orkut.OrkutUser;

public class OrkutUtil {

	public static OrkutScrapDetails getOrkutScraps(GetScrapsTx pScrapTx){
			OrkutScrapDetails scrapDetails = new OrkutScrapDetails();
			int count = pScrapTx.getScrapCount();
			scrapDetails.setCount(count);
			
			count = count > 20 ? 20 : count;
			List<OrkutScrap> scraps = new ArrayList<OrkutScrap>();
			for(int i = 0; i < count;i++){
				scraps.add(getOrkutScrap(pScrapTx.getScrap(i)));
			}
			scrapDetails.setScraps(scraps);
			return scrapDetails;
		}

	public static OrkutScrap getOrkutScrap(ScrapEntry pScrapEntry) {
		OrkutScrap scrap = new OrkutScrap();
		scrap.setUser(getOrkutUser(pScrapEntry.getFromUserProfile()));
		scrap.setBody(pScrapEntry.getBody());
		return scrap;
	}

	public static OrkutUser getOrkutUser(OrkutPerson person) {
		OrkutUser userInfo = new OrkutUser();
		userInfo.setThumbNailUrl(person.getThumbnailUrl());
		userInfo.setDisplayName(person.getDisplayName());
		userInfo.setId(person.getId());
		userInfo.setStatus(person.getStatus());
		return userInfo;
	}

	public static OrkutBirthdayDetails getBirthdayNotifications(
			BirthdayNotificationTx pBirthdayNotification) {
		OrkutBirthdayDetails birthdayDetails = new OrkutBirthdayDetails();
		int count = pBirthdayNotification.getBirthdayNotificationCount();
		birthdayDetails.setCount(count);
		
		count = count > 50? 50 : count;
		
		List<OrkutBirthday> birthdays = new ArrayList<OrkutBirthday>();
		for(int i = 0; i< count; i++){
			birthdays.add(getOrkutBirthday(pBirthdayNotification.getBirthdayNotification(i)));
		}
		birthdayDetails.setBirthdays(birthdays);
		return birthdayDetails;
		
	}

	private static OrkutBirthday getOrkutBirthday(
			BirthdayNotificationEntry pBirthdayNotification) {
		OrkutBirthday birthday = new OrkutBirthday();
		birthday.setDay(pBirthdayNotification.getBirthDay());
		birthday.setMonth(pBirthdayNotification.getBirthMonth());
		birthday.setUser(getOrkutUser(pBirthdayNotification.getUserProfile()));
		return birthday;
	}

	public static OrkutFriendDetails getOrkutFriends(GetFriendTx pGetFriendsTx) {
		OrkutFriendDetails friendDetails = new OrkutFriendDetails();
		int count = pGetFriendsTx.getFriendsCount();
		friendDetails.setCount(count);
		
		count = count > 50? 50: count;
		List<OrkutUser> friends = new ArrayList<OrkutUser>(count);
		
		for(int i=0;i<count;i++){
			friends.add(getOrkutUser(pGetFriendsTx.getFriend(i)));
		}
		friendDetails.setFriends(friends);
		
		return friendDetails;
	}

}
